package com.csjy.migration.hnnd.business;

import com.csjy.migration.hnnd.BusinessTool;
import com.csjy.migration.hnnd.DbUtil;
import com.csjy.migration.hnnd.ProgramArgument;
import com.csjy.migration.hnnd.Tool;
import com.csjy.migration.hnnd.annotation.BusinessDataConverter;

import javax.script.ScriptException;
import java.io.FileWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 工作移交记录
 */
@BusinessDataConverter(
        insertSql = "insert into nd_project_handover_record (ID, CREATE_NAME, CREATE_BY, CREATE_DATE, UPDATE_NAME, UPDATE_BY, UPDATE_DATE, SYS_ORG_CODE, SYS_COMPANY_CODE, BPM_STATUS, PRO_ID, MANAGER_TYPE, SRC_MANAGER_ID, SRC_MANAGER_NAME, DES_MANAGER_A, EFFECTIVE_DATE, REMARK, DES_MANAGER_B, DES_MANAGER_C, DES_MANAGER_D, BRANCH_COMPANY, FORMER_HEAD_OF, TRANSFER_OFFICE, DIRECTOR, TURN_COMPANY, PRO_CID, Office)" +
                "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
        updateSql = "UPDATE nd_project_handover_record SET CREATE_NAME = ?,CREATE_BY = ?,CREATE_DATE = ?,UPDATE_NAME = ?,UPDATE_BY = ?,UPDATE_DATE = ?,SYS_ORG_CODE = ?,SYS_COMPANY_CODE = ?,BPM_STATUS = ?,PRO_ID = ?,MANAGER_TYPE = ?,SRC_MANAGER_ID = ?,SRC_MANAGER_NAME = ?,DES_MANAGER_A = ?,EFFECTIVE_DATE = ?,REMARK = ?,DES_MANAGER_B = ?,DES_MANAGER_C = ?,DES_MANAGER_D = ?,BRANCH_COMPANY = ?,FORMER_HEAD_OF = ?,TRANSFER_OFFICE = ?,DIRECTOR = ?,TURN_COMPANY = ?,PRO_CID = ?,OFFICE = ? WHERE ID = ?",
        findMigratedIdListSql = "SELECT ID FROM nd_project_handover_record WHERE PRO_ID = ?"
)
public class NdProjectHandoverRecord extends AbstractBusiness{

    public NdProjectHandoverRecord(DbUtil sourceDbUtil, DbUtil targetDbUtil, SourceBusinessData sourceData, ProgramArgument programArgument, String projectId) throws SQLException {
        super(sourceDbUtil, targetDbUtil, sourceData, programArgument, projectId);
    }

    @Override
    public void migrate() throws ScriptException, SQLException, Exception {
        List<Map> BtItemMangerChangeList = sourceData.getBtItemManagerChange();
        for(Map btItemMangerChange:BtItemMangerChangeList){
            try {
                String sourceId = btItemMangerChange.get("SORTNO").toString();
                int needMigratedResult = this.needMigrated(sourceId);
                if(needMigratedResult == 0) {
                    continue;
                };
                // 单条插入或更新
                if (needMigratedResult == 1) {
                    this.targetDbUtil.updateByParams(updateSql, this.buildValues(btItemMangerChange), true);
                    // 批量插入或更新 和 单条插入或更新 二选一
//             this.batchUpdateValues.add(this.buildValues(row));
                } else {
                    this.targetDbUtil.updateByParams(insertSql, this.buildValues(btItemMangerChange));
                    // 批量插入或更新 和 单条插入或更新 二选一
//             this.batchInsertValues.add(this.buildValues(row));
                }

            } catch (Exception e) {
                throw new Exception(String.format("工作移交记录[%s]迁移失败:%s\n", btItemMangerChange.get("ITEMNO"), e.getMessage()));
            }

        }

    }
    public List buildValues (Map BtItemMangerChange) throws SQLException, ScriptException {
        String  newManager = Tool.removeOrgCodeFromEmpCode(BtItemMangerChange.get("NEWMANAGER").toString());
        List values = new ArrayList();
        //ID
        values.add(BtItemMangerChange.get("SORTNO").toString());
        //CREATE_NAME	创建人名称
        values.add(null);
        //CREATE_BY	创建人登录名称
        values.add(null);
        //CREATE_DATE	创建日期
        values.add(BtItemMangerChange.get("CREATE_TIME"));
        //UPDATE_NAME	更新人名称
        values.add(null);
        //UPDATE_BY	更新人登录名称
        values.add(null);
        //UPDATE_DATE	更新日期
        values.add(BtItemMangerChange.get("MODIFY_TIME"));
        //SYS_ORG_CODE	所属部门
        values.add(null);
        //SYS_COMPANY_CODE	所属公司
        values.add("002");
        //BPM_STATUS	流程状态
        values.add(1);
        //PRO_ID	项目ID
        values.add(BtItemMangerChange.get("ITEMNO"));
        //MANAGER_TYPE	经理类别
        String ManagerType = BtItemMangerChange.get("MANTYPE").toString();
        if(BtItemMangerChange.get("MANTYPE").toString().equals("T经理") ||BtItemMangerChange.get("MANTYPE").toString().equals("R经理")){
            ManagerType = "D经理";
        }
        values.add(ManagerType);
        //SRC_MANAGER_ID	原项目经理ID
        values.add(BtItemMangerChange.get("OLDMANAGER"));
        //SRC_MANAGER_NAME	原项目经理姓名
        values.add(BusinessTool.getEmpNameByEmpCode(Tool.removeOrgCodeFromEmpCode(BtItemMangerChange.get("OLDMANAGER").toString())));
        //DES_MANAGER_A	新项目A经理姓名
        if(BtItemMangerChange.get("MANTYPE").toString().equals("A经理")){
            values.add(newManager);
        }else {
            values.add(null);
        }

        //EFFECTIVE_DATE	生效时间
        values.add(BtItemMangerChange.get("OCCURTIME"));
        //REMARK	账目备注
        String Remark = Tool.decodeUnicode(BtItemMangerChange.get("MEMO").toString());
        if(BtItemMangerChange.get("MANTYPE").toString().equals("T经理") || BtItemMangerChange.get("MANTYPE").toString().equals("R经理")){
            Remark = "经理类型:"+BtItemMangerChange.get("MANTYPE").toString()+Remark;
        }
        values.add(Remark);
        //DES_MANAGER_B	新项目B经理姓名
        String DesManagerB = null;
        if(BtItemMangerChange.get("MANTYPE").toString().equals("B经理")){
            values.add(newManager);
        }else{
            values.add(null);
        }
        //DES_MANAGER_C	新项目C经理姓名
        String DesManagerC = null;
        if(BtItemMangerChange.get("MANTYPE").toString().equals("C经理")){
            values.add(newManager);
        }else {
            values.add(null);
        }
        //DES_MANAGER_D	新项目D经理姓名
        String DesManagerD = null;
        if(BtItemMangerChange.get("MANTYPE").toString().equals("D经理") ||BtItemMangerChange.get("MANTYPE").toString().equals("T经理") || BtItemMangerChange.get("MANTYPE").toString().equals("R经理")){
            values.add(newManager);
        }else{
            values.add(null);
        }
        //BRANCH_COMPANY	所属分公司
        values.add(null);
        //FORMER_HEAD_OF	原负责人
        values.add(null);
        //TRANSFER_OFFICE	移交办事处
        values.add(null);
        //DIRECTOR	新负责人
        values.add(null);
        //TURN_COMPANY	移交分公司
        values.add(null);
        //PRO_CID	流程id
        values.add(BtItemMangerChange.get("WORKFLOWNO") == null ? null :BtItemMangerChange.get("WORKFLOWNO")+"-JW");
        //Office	原办事处
        values.add(null);
        return values;
    }
}



